package org.example.service.util.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.UtilInfoMapper;
import org.example.model.common.AppHttpCodeEnum;
import org.example.model.common.ResponseResult;
import org.example.model.user.dtos.UserIdentityDto;
import org.example.model.util.dtos.UtilInfoDto;
import org.example.model.util.entities.UtilInfo;
import org.example.model.util.vos.ModifyUtilInfoVo;
import org.example.service.util.UtilInfoService;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 常用工具信息服务层实现
 */
@Service
@Transactional
@Slf4j
public class UtilInfoServiceImpl extends ServiceImpl<UtilInfoMapper, UtilInfo> implements UtilInfoService {

    @Autowired
    private UtilInfoMapper utilInfoMapper;

    /**
     * 添加一条工具信息
     */
    @Override
    public ResponseResult<Boolean> InsertUtilInfo(ModifyUtilInfoVo modifyUtilInfoVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (modifyUtilInfoVo == null) {
            // 缺少参数
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        UtilInfo utilInfo = new UtilInfo();
        BeanUtils.copyProperties(modifyUtilInfoVo, utilInfo);
        this.HandleModifyUtilInfo(utilInfo);
        Boolean flag = utilInfoMapper.insert(utilInfo) > 0;
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 根据id修改一条工具信息
     */
    @Override
    public ResponseResult<Boolean> UpdateUtilInfoById(ModifyUtilInfoVo modifyUtilInfoVo) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (modifyUtilInfoVo == null || modifyUtilInfoVo.getId() <= 0) {
            // 缺少参数 工具id
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        UtilInfo utilInfo = new UtilInfo();
        BeanUtils.copyProperties(modifyUtilInfoVo, utilInfo);
        this.HandleModifyUtilInfo(utilInfo);
        Boolean flag = utilInfoMapper.updateById(utilInfo) > 0;
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 添加或修改日报信息统一操作
     */
    private void HandleModifyUtilInfo (UtilInfo utilInfo) {
        // 获取进程中的用户数据
        UserIdentityDto userIdentityDto = UserIdThreadLocalUtils.getUser();
        if (utilInfo.getId() == 0) {
            utilInfo.setCreateUserId(userIdentityDto.getId());
            utilInfo.setCreateTime(LocalDateTime.now());
            utilInfo.setIsDeleted(false);
        }
        utilInfo.setUpdateUserId(userIdentityDto.getId());
        utilInfo.setUpdateTime(LocalDateTime.now());
    }

    /**
     * 根据id删除一条工具信息
     */
    @Override
    public ResponseResult<Boolean> DeleteUtilInfoById(Integer id) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        // 参数审查
        if (id <= 0) {
            // 缺少参数 工具信息id
            result.error(AppHttpCodeEnum.PARAM_REQUIRE.getCode(), AppHttpCodeEnum.PARAM_REQUIRE.getErrorMessage());
            return result;
        }
        UtilInfo utilInfo = utilInfoMapper.selectById(id);
        utilInfo.setIsDeleted(true);
        this.HandleModifyUtilInfo(utilInfo);
        Boolean flag = utilInfoMapper.updateById(utilInfo) > 0;
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), flag, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 获取所有的工具信息列表
     */
    @Override
    public ResponseResult<List<UtilInfoDto>> GetUtilInfoList() {
        ResponseResult<List<UtilInfoDto>> result = new ResponseResult<>();
        LambdaQueryWrapper<UtilInfo> utilQuery = new LambdaQueryWrapper<>();
        utilQuery.eq(UtilInfo:: getIsDeleted, false);
        List<UtilInfo> utilInfos = utilInfoMapper.selectList(utilQuery);
        List<UtilInfoDto> utilInfoDtoList = new ArrayList<>();
        UtilInfoDto utilInfoDto = null;
        for (UtilInfo utilInfo : utilInfos) {
            utilInfoDto = new UtilInfoDto();
            BeanUtils.copyProperties(utilInfo, utilInfoDto);
            utilInfoDtoList.add(utilInfoDto);
        }
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), utilInfoDtoList, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }
}
